package com.hxkcloud.patient.activitys;

import android.os.Message;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.adapter.TransactionAdapter;
import com.hxkcloud.patient.beans.OrderBean;
import com.hxkcloud.patient.beans.OrderListBean;
import com.hxkcloud.patient.request.MessageCode;
import com.hxkcloud.patient.request.YiJiaHuNetWorkUtils;
import com.hxkcloud.patient.views.pulltorefreshlayout.PullToRefreshLayout;
import com.hxkcloud.patient.views.pulltorefreshlayout.PullableListView;

import java.util.ArrayList;
import java.util.List;

public class TransactionActivity extends BaseActivity implements View.OnClickListener,PullToRefreshLayout.OnRefreshListener {

    private LinearLayout ll_title;
    private ImageView iv_back;
    private TextView tv_title;
    private TextView tv_collect;

    private PullableListView listView_transtion;
    private TransactionAdapter adapter;
    private OrderListBean orderList;
    private List<OrderBean> datas;

    private PullToRefreshLayout refreshLayout;
    private int totalCount = 0;
    private int pageIndex = 1;
    private int pageSize = 15;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_transaction);
        datas = new ArrayList<>();
        initView();
        initData();
        initListener();
    }

    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what){
            case MessageCode.NETWORKCODE_GETUSERORDERLIST_SUCCESS:
                if(msg.obj!=null){
                    dismisssLoading();
                    orderList = (OrderListBean) msg.obj;
                    if (pageIndex==1){
                        datas.clear();
                        refreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                        if (orderList.getOrderList().size()>0){
                            refreshLayout.refreshFinish(View.VISIBLE);
                        }else {
                            refreshLayout.refreshFinish(View.GONE);
                        }

                    }else {
                        refreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
                    }
//                    if (orderList.getOrderList().size()==0){
//                        if (isLoadMore){
//                            refreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
//                        }else {
//                            datas.clear();
//                            adapter.setData(datas);
//                            refreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
//                        }
//                    }else {
//                        if (isLoadMore){
//
//                        }else {
//                            datas.clear();
//                        }
//                        datas.addAll(orderList.getOrderList());
//                        adapter.setData(datas);
//                        if (isLoadMore) {
//                            refreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
//                        } else {
//                            refreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
//                        }
//
//                    }
//                    if (orderList.getOrderList()==null||orderList.getOrderList().size()==0){
//                        toastMsg("暂无数据");
//                    }else {
//                        for (int i=0;i<orderList.getOrderList().size();i++){
//                            datas.add(orderList.getOrderList().get(i));
//                        }
//                    }

//                    orderBeen = orderList.getOrder();
                    datas.addAll(orderList.getOrderList());
                    totalCount = orderList.getTotalCount();
                    Log.i("-----datas---->",datas.toString());
                }
                adapter.notifyDataSetChanged();
                break;
            case MessageCode.NETWORKCODE_GETUSERORDERLIST_FAILURE:
                if (pageIndex == 1) {
                    refreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                }
                else {
                    refreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
                }
                if ((msg.obj != null) && (!TextUtils.isEmpty((String) msg.obj))) {
                    toastMsg((String) msg.obj);
                } else {
                    toastMsg(R.string.datagetfail);
                }
                break;
        }
    }

    @Override
    protected void initView() {
        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        iv_back = (ImageView) findViewById(R.id.iv_title_back);
        tv_title = (TextView) findViewById(R.id.tv_title_title);
        tv_collect = (TextView) findViewById(R.id.tv_title_func);

        setTitlePadding(ll_title);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(this);
        tv_title.setText("交易记录");

        listView_transtion = (PullableListView) findViewById(R.id.transaction_lv);
        refreshLayout = (PullToRefreshLayout) findViewById(R.id.transaction_refreshlayout);
        refreshLayout.setCanPull(true,true);
    }

    @Override
    protected void initListener() {
        iv_back.setOnClickListener(this);
        refreshLayout.setOnRefreshListener(this);
    }

    @Override
    protected void initData() {


        adapter = new TransactionAdapter(mContext,datas);
        listView_transtion.setAdapter(adapter);
    }

    @Override
    public void onResume() {
        super.onResume();
        loading();
//        pageIndex = 1;
        YiJiaHuNetWorkUtils.GetUserOrderList(loginInfo.getUserId(),pageIndex,pageSize,handler);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_title_back:
                finish();
                break;
        }
    }

    @Override
    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
        pageIndex = 1;
        loading();
        YiJiaHuNetWorkUtils.GetUserOrderList(loginInfo.getUserId(),pageIndex,pageSize,handler);
    }

    @Override
    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
        int dataSiz = 0;
        for (OrderBean bean:datas){
            dataSiz+=1;
        }
        if (dataSiz>=totalCount){
            toastMsg("没有更多了");
            refreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
        }else {
            pageIndex++;
            loading();
            YiJiaHuNetWorkUtils.GetUserOrderList(loginInfo.getUserId(),pageIndex,pageSize,handler);
        }

    }
}
